package com.hotdog.test;

import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;

import java.io.IOException;

/**
 * @Author wangzhengnian
 * @Date 2021/3/16 14:39
 * @Version 1.0
 */
public class AttachMain {
    public static void main(String[] args) throws IOException, AgentLoadException, AgentInitializationException, AttachNotSupportedException, InstantiationException, IllegalAccessException {
        Integer pid = 47938;
        //编译java
        String javaPath = "/Users/wangzhengnian/develop/company/test/poly/poly-web/src/main/java/cn/poly/PolyApplication.java";

        System.out.println("当前需要热更新 jvm pid 为 " + pid);
        // 获取当前 jar 路径
//        URL jarUrl = AttachMain.class.getProtectionDomain().getCodeSource().getLocation();
        String jarPath = "/Users/wangzhengnian/develop/company/test/hotdog/agent/build/libs/hotdog-agent.jar";
        System.out.println("当前热更新工具 jar 路径为 " + jarPath);
        VirtualMachine vm = VirtualMachine.attach(String.valueOf(pid));
        // 运行最终 AgentMain 中方法
         vm.loadAgent(jarPath, javaPath);
    }


}
